package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.*;
import com.example.mapper.NurseMapper;
import com.example.mapper.ReserveMapper;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReserveService {
   @Autowired
   private ReserveMapper reserveMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private NurseMapper nurseMapper;


    public void add(Reserve reserve) {
        reserveMapper.add(reserve);
    }


    public PageInfo<Reserve> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());


        List<Reserve> list = reserveMapper.findBySearch(params);
        if (CollectionUtil.isEmpty(list)) {
            return PageInfo.of(new ArrayList<>());
        }
        for (Reserve reserve : list) {
            if (ObjectUtil.isNotEmpty(reserve.getServiceid())) {
                Nurse nurse = nurseMapper.selectById(reserve.getServiceid());
                if (ObjectUtil.isNotEmpty(nurse)) {
                    reserve.setServiceName(nurse.getName());
                }
            }
            if (ObjectUtil.isNotEmpty(reserve.getUserid())) {
                User user = userMapper.selectById(reserve.getUserid());
                if (ObjectUtil.isNotEmpty(user)) {
                    reserve.setUserName(user.getName());
                }
            }
        }
        return PageInfo.of(list);
    }


    public void delete(Integer id) {
        reserveMapper.deleteById(id);
    }
}
